package rstplus;

/*
 * RST - Russian SMS transliterator
 * Copyleft 2011
 * Author: Alexey Shat'ko (Алексей Шатько), rstapp@yandex.ru
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this source code (in file copying.txt); if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

public final class SMSSender {

    public static final int OK = 0;
    public static final int ERROR = 1;
    public static final int INVALID_NUMBER = 2;
    public static final int TOO_LONG_MESSAGE = 3;

    private SMSSender() {
    }

    public static boolean checkNumber(String number) {
        int len = number.length();
        if (len < 6) {
            return false;
        }
        char ch1 = number.charAt(0);
        if ((!Character.isDigit(ch1)) && (ch1 != '+')) {
            return false;
        }
        for (int i = 1; i < len; i++) {
            if (!Character.isDigit(number.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static int send(String message, String number) {
        if (!checkNumber(number)) {
            return INVALID_NUMBER;
        }
        try {
            MessageConnection mc = (MessageConnection) Connector.open("sms://" + number);
            try {
                TextMessage tm = (TextMessage) mc.newMessage(MessageConnection.TEXT_MESSAGE);
                tm.setPayloadText(message);
                if (mc.numberOfSegments(tm) > 3) {
                    return TOO_LONG_MESSAGE;
                }
                mc.send(tm);
            } finally {
                mc.close();
            }
            return OK;
        } catch (Exception exc) {
            return ERROR;
        }
    }
    public static final int TOO_MANY_PARTS = 10;

    public static final class PartsCount {

        private int count;
        private int remains;

        PartsCount(int count, int remains) {
            this.count = count;
            this.remains = remains;
        }

        public int getCount() {
            return count;
        }

        public int getRemainded() {
            return remains;
        }

        public String toString() {
            return (String) ((count == TOO_MANY_PARTS) ? "!" : String.valueOf(count)) + ":"
                    + String.valueOf(remains);
        }

        public String toString1(PartsCount compareTo) {
            if ((compareTo.count == this.count) && (compareTo.remains == this.remains)) {
                return (String) ((count == TOO_MANY_PARTS) ? "!" : String.valueOf(count)) + ":"
                        + "?";
            } else {
                return (String) ((count == TOO_MANY_PARTS) ? "!" : String.valueOf(count)) + ":"
                        + String.valueOf(remains);
            }
        }
    }

    public static PartsCount getPartsCount(String str) {
        boolean isGSM7 = true;
        int len = str.length();
        int count = len;
        _loop:
        for (int i = 0; i < len; i++) {
            _switch:
            switch (getCharType(str.charAt(i))) {
                case ONE_BYTE_CHAR:
                    break _switch;
                case TWO_BYTE_CHAR:
                    count++;
                    break _switch;
                default:
                    isGSM7 = false;
                    break _loop;
            }
        }
        if (isGSM7) {
            if (count <= 160) {
                return new PartsCount(1, 160 - count);
            } else if (count <= 304) {
                return new PartsCount(2, 304 - count);
            } else if (count <= 456) {
                return new PartsCount(3, 456 - count);
            } else {
                return new PartsCount(TOO_MANY_PARTS, 456 - count);
            }
        } else {
            if (len <= 70) {
                return new PartsCount(1, 70 - len);
            } else if (len <= 132) {
                return new PartsCount(2, 132 - len);
            } else if (len <= 198) {
                return new PartsCount(3, 198 - len);
            } else {
                return new PartsCount(TOO_MANY_PARTS, 198 - len);
            }
        }
    }
    public static final int ONE_BYTE_CHAR = 1;
    public static final int TWO_BYTE_CHAR = 2;
    public static final int UCS2_CHAR = 3;

    public static int getCharType(char ch) {
        if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9'))) {
            return ONE_BYTE_CHAR;
        }
        switch (ch) {
            case '@':
            case '£':
            case '$':
            case '¥':
            case 'è':
            case 'é':
            case 'ù':
            case 'ì':
            case 'ò':
            case 'Ç':
            case '\n':
            case 'ø':
            case 'Ø':
            case '\r':
            case 'Å':
            case 'å':
            case 'Δ':
            case '_':
            case 'Φ':
            case 'Γ':
            case 'Λ':
            case 'Ω':
            case 'Π':
            case 'Ψ':
            case 'Σ':
            case 'Θ':
            case 'Ξ':
            case 'Æ':
            case 'æ':
            case 'ß':
            case 'É':
            case ' ':
            case '!':
            case '\"':
            case '#':
            case '¤':
            case '%':
            case '&':
            case '\'':
            case '(':
            case ')':
            case '*':
            case '+':
            case ',':
            case '-':
            case '.':
            case '/':
            case ':':
            case ';':
            case '<':
            case '=':
            case '>':
            case '?':
            case '¡':
            case 'Ä':
            case 'Ö':
            case 'Ñ':
            case 'Ü':
            case '§':
            case '¿':
            case 'ä':
            case 'ö':
            case 'ñ':
            case 'ü':
            case 'à':
                return ONE_BYTE_CHAR;
            case '^':
            case '{':
            case '}':
            case '\\':
            case '[':
            case '~':
            case ']':
            case '|':
            case '€':
                return TWO_BYTE_CHAR;
            default:
                return UCS2_CHAR;
        }
    }
}
